package com.cictec.middleware.core.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cictec.middleware.commons.mybatis.dao.MybatisDaoUtil;
import com.cictec.middleware.core.entity.Bus;
import com.cictec.middleware.core.entity.Device;
import com.cictec.middleware.core.mapper.DeviceMapper;

import tk.mybatis.mapper.entity.Example;

/**
 * 设备服务
 * @author ql
 *
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Service
public class DeviceService {

	private static Logger logger = LoggerFactory.getLogger(DeviceService.class);
	
	@Autowired
	private DeviceMapper deviceMapper;
	private MybatisDaoUtil<Device> dao = new MybatisDaoUtil<Device>();
	
	/**
	 * 获取所有有效设备
	 * @return
	 */
	public List<Device> selectListDevice() {
		Example example = new Example(Device.class);
		example.createCriteria().andEqualTo("devIsvalid", "1").andEqualTo("devDropFlag", "0");
		
		List<Device> list = deviceMapper.selectByExample(example);
		
		return list;
	}
	
	/**
	 * 根据设备uuid查询设备
	 * 
	 * @param devUuid
	 * @return
	 */
	public Device selectDeviceByDevUuid(String devUuid) {
		Device device = deviceMapper.selectByPrimaryKey(devUuid);
		return device;
	}

	/**
	 * 根据设备编号查询设备
	 * 
	 * @param devCode
	 * @return
	 */
	public Device selectDeviceByDevCode(String devCode) {
		Device device = new Device();
		device.setDevCode(devCode);
		device.setDevDropFlag("0");
		return deviceMapper.selectOne(device);
	}
	
	/**
	 * 根据车辆uuid查询设备
	 * @param busUuid
	 * @return
	 */
	public Device selectDeviceByBusUuid(String busUuid) {
		
		Map params = new HashMap();
		params.put("busUuid", busUuid);
		return selectOneBusByParams(params);
	}
	
	public Device selectOneBusByParams(Map params) {
		return dao.selectOne("com.cictec.middleware.core.mapper.DeviceMapper.selectDeviceByParams", params);
	}
	

	/**
	 * 根据序列号查询设备编号
	 * 
	 * @param serialNumber
	 * @return
	 */
	public Device selectDeviceByRefId(String serialNumber) {
		Device device = new Device();
		device.setDevRefId(serialNumber);
		device.setDevDropFlag("0");
		device = deviceMapper.selectOne(device);

		return device;

	}

	/**
	 * 更新设备在线状态
	 * 
	 * @param devUuid
	 * @param devOnlineStatus
	 */
	public void updateDeviceOnlineStatusByDevUuid(String devUuid, String devOnlineStatus) {
		logger.debug("【设备管理服务】【更新设备在线状态】devUuid:{}  status:{}", devUuid, devOnlineStatus);
		Device device = new Device();
		device.setDevUuid(devUuid);
		device.setDevOnlineStatus(devOnlineStatus);
//		device.setDevUpdateTime(new Date());
		device.setDevStatusChangeTime(new Date());
		deviceMapper.updateByPrimaryKeySelective(device);
	}

	/**
	 * 更新设备序列号 <br>
	 * 数据库对应字段 dev_ref_id
	 * 
	 * @param devUuid
	 * @param serialNumber
	 */
	public void updateDeviceSerialNumber(String devUuid, String serialNumber) {
		logger.debug("【设备管理服务】【更新设备序列号】devUuid:{}  serialNumber:{}", devUuid, serialNumber);
		Device device = new Device();
		device.setDevUuid(devUuid);
		device.setDevRefId(serialNumber);
		device.setDevUpdateTime(new Date());
		deviceMapper.updateByPrimaryKeySelective(device);
	}

}
